package address;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.LineBorder;

/**
 * @author Mavis
 */
public class CreatPeople extends JFrame {

    People people = null;
    Term termname = new Term(0, "姓名：");
    Term[] termphone = new Term[20];
    Term termbirth = new Term(6, "生日： ");
    Term termremark = new Term(3, "备注：");
    Term[] termemail = new Term[20];
    Term[] termIm = new Term[20];
    Term termhome = new Term(4, "家庭住址：");
    Term termwork = new Term(4, "工作住址：");
    Term termpage = new Term(4, "个人主页：");
    Term termpost = new Term(0, "邮编：");
//    JButton jbtkeep = new JButton("  保存 ");
//    JButton jbtcancel = new JButton("  取消 ");
    CreatPanel creatPanel = null;
    Font f = new Font("微软雅黑", Font.PLAIN, 16);
    JLabel label = new JLabel("仅支持jpg或者gif格式");
    
    JButton photo;//btn上传照片
    ArrayList list = null;   //记录联系人的线性表，里面包含所有联系人
    ArrayList belongs = null;   //记录分组信息
    bbgPanel ppanel=new bbgPanel();

    public CreatPeople(ArrayList List, ArrayList belongs) {
        ImageIcon Icon_upload_phpto = new ImageIcon("img/upload_phpto.png");
        this.photo = new JButton(Icon_upload_phpto);
   
        ppanel.setLayout(null);
        add(ppanel);
        this.list = List;
        this.belongs = belongs;
        creatPanel = new CreatPanel();
        creatPanel.setBounds(0, 0, 520, 500);
        creatPanel.setOpaque(false); 
        add(creatPanel,0);
        creatPanel.setFocusable(true);

        //添加保存按钮
        ImageIcon Icon1 = new ImageIcon("img/save_newPeople.png"); 
        JButton jbtkeep = new JButton(Icon1);
        jbtkeep.setContentAreaFilled(false);
        jbtkeep.setBounds(100, 508, 100, 30);
        jbtkeep.addActionListener(new KeepActionListener());
        add(jbtkeep);

        //添加取消按钮
        ImageIcon Icon2 = new ImageIcon("img/cancel_newPeople.png"); 
        JButton jbtcancel = new JButton(Icon2);
        jbtcancel.setContentAreaFilled(false);
        jbtcancel.setBounds(300, 508, 100, 30);
        jbtcancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                BeginPanel frame = new BeginPanel(list, belongs);
                frame.setSize(900, 700);
                frame.setTitle("欢迎使用通讯录管理系统");
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                dispose();
            }

        });
        add(jbtcancel);
         add(ppanel,-1);
    }

    class CreatPanel extends JPanel {

        JPanel panel = new JPanel();
        showPanel showpanel = new showPanel();
        ImageIcon iconChooseGroup = new ImageIcon("img/choose_group.png");
        JButton jbtbelongs = new JButton(iconChooseGroup);
   
        JButton jbt = null;
        bgPanel p = new bgPanel();
       
        CreatPanel() {
            setLayout(null);
            people = new People();
            panel.setLayout(null);
            panel.setOpaque(false); 
            JScrollPane jscp = new JScrollPane();
            jscp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            panel.setPreferredSize(new Dimension(500, 800));
            jscp.setViewportView(panel);
            jscp.setBounds(0, 0, 500, 485);
            jscp.setOpaque(false); 
            jscp.getViewport().setOpaque(false);
            add(jscp,0);
            
            showpanel.add(p);
            p.setBackground(Color.white);
            showpanel.add(label);
            showpanel.setOpaque(false); 
           
            showpanel.add(photo);
            photo.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    JFileChooser photoChooser = new JFileChooser();
                    File f;
                    if (photoChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                        f = photoChooser.getSelectedFile();
                        people.photo = f.getPath();
                        p.setbg(f.getPath());
                     
                    }
                    p.repaint();
                }

            });

            for (int i = 0; i < 20; i++) {
                termphone[i] = new Term(5, " ");
                termphone[i].panel.setVisible(true);
                showpanel.add(termphone[i].panel);
                termphone[i].jbtAdd.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {

                        showpanel.refresh();
                    }

                });
                termphone[i].jbtSub.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        showpanel.refresh();
                    }

                });
            }
            for (int i = 0; i < 20; i++) { 
                termemail[i] = new Term(1, "电子邮箱：");    
                termemail[i].panel.setVisible(true);
                showpanel.add(termemail[i].panel);
                termemail[i].jbtAdd.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {

                        showpanel.refresh();
                    }

                });
                termemail[i].jbtSub.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        showpanel.refresh();
                    }

                });
            }
            for (int i = 0; i < 20; i++) {
                termIm[i] = new Term(2, " ");
                termIm[i].panel.setVisible(true);
                showpanel.add(termIm[i].panel);
                termIm[i].jbtAdd.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {

                        showpanel.refresh();
                    }

                });
                termIm[i].jbtSub.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        showpanel.refresh();
                    }

                });
            }
            showpanel.setBounds(0, 0, 550, 500);
            panel.add(showpanel);
            showpanel.add(termname.panel);
            showpanel.add(termbirth.panel);
            showpanel.add(termremark.panel);
            jbtbelongs.setBorderPainted(false); 
            jbtbelongs.setOpaque(false); 
            showpanel.add(jbtbelongs);
            jbtbelongs.addActionListener(
                    new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent ae) {
                            SelectBelongs sframe = new SelectBelongs(belongs, people);
                            sframe.setSize(420, 430);
                            sframe.setTitle("选择分组");
                            sframe.setLocationRelativeTo(null);
                            sframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            sframe.setVisible(true);
                        }

                    });
            showpanel.add(termwork.panel);
            showpanel.add(termhome.panel);
            showpanel.add(termwork.panel);
            showpanel.add(termpage.panel);
            showpanel.add(termpost.panel);
            add(ppanel,-1);
        }

        class showPanel extends JPanel {

            public void refresh() {
                repaint();
            }

            protected void paintComponent(Graphics g) {

                super.paintComponent(g);
                int height = 20;

                //添加照片栏    
                if (people.photo != null) {
                    p.setbg(people.photo);
                }
                p.setBounds(350, 20, 100, 110);
                p.setBorder(new LineBorder(Color.black, 1));
                Font font1 = new Font("微软雅黑", Font.PLAIN, 12);
                label.setFont(font1);
                label.setBounds(345, 138, 150, 25);
                
                photo.setBorderPainted(false);
                photo.setBounds(350, 170, 100, 30);

                //添加姓名栏
                termname.panel.setBounds(20, height, 400, 25);   
                height += 35;

                //添加生日栏
                termbirth.panel.setBounds(20, height, 400, 25);
                height += 35;

                //添加备注栏
                termremark.panel.setBounds(20, height, 400, 80);
                height += 95;

                //添加分组按钮
                jbtbelongs.setBounds(350,215, 100, 30);
           

                //添加联系号码栏
                for (int i = 0; i <= Term.numphone; i++) {
                    if (termphone[i].panel.isVisible()) {
                        termphone[i].panel.setBounds(20, height, 400, 25);
                        height = height + 35;
                    }

                }

                //添加电子邮箱栏
                for (int i = 0; i <= Term.numemail; i++) {
                    if (termemail[i].panel.isVisible()) {
                        termemail[i].panel.setBounds(20, height, 400, 25);
                        height = height + 35;
                    }

                }

                //添加即使通讯工具栏  
                for (int i = 0; i <= Term.numIm; i++) {
                    if (termIm[i].panel.isVisible()) {
                        termIm[i].panel.setBounds(20, height, 400, 25);
                        height = height + 35;
                    }

                }

                //添加家庭住址栏
                termhome.panel.setBounds(20, height, 400, 25);
                height += 35;

                //添加工作住址栏 
                termwork.panel.setBounds(20, height, 400, 25);
                height += 35;

                //添加个人主页栏    
                termpage.panel.setBounds(20, height, 400, 25);
                height += 35;

                //添加邮政编码栏
                termpost.panel.setBounds(20, height, 400, 25);
                height += 35;

            }

        }

    }

    private class KeepActionListener implements ActionListener {

        public KeepActionListener() {
        }

        @Override
        public void actionPerformed(ActionEvent ae) {

            int i;
            File f;
            IM im = null;
            Phone phone = null;
            people.name = termname.text.getText();
            people.homepage = termpage.text2.getText();
            people.remark = termremark.text1.getText();
            people.workplace = termwork.text2.getText();
            people.home = termhome.text2.getText();
            people.postcode = termpost.text.getText();
            people.day = termbirth.textday.getText();
            people.year = termbirth.textyear.getText();
            people.month = termbirth.textmonth.getText();
            String s = new String();
            for (i = 0; i <= Term.numphone; i++) {
                s = termphone[i].text.getText();
                if (s.length() != 0 && termphone[i].panel.isVisible()) {
                    phone = new Phone((String) termphone[i].phone.getSelectedItem(), s);
                    people.phone.add(phone);
                }
            }
            for (i = 0; i <= Term.numemail; i++) {
                s = termemail[i].text.getText();
                if (s.length() != 0 && termemail[i].panel.isVisible()) {
                    people.email.add(s);
                }
            }
            for (i = 0; i <= Term.numIm; i++) {
                s = termIm[i].text.getText();
                if (s.length() != 0 && termIm[i].panel.isVisible()) {
                    im = new IM((String) termIm[i].im.getSelectedItem(), s);
                    people.im.add(im);
                }

            }

            if (people.name.length() == 0) {
                JOptionPane.showMessageDialog(null, "姓名栏不能为空", "注意", JOptionPane.INFORMATION_MESSAGE);
            } else if (people.phone.size() == 0) {
                JOptionPane.showMessageDialog(null, "联系号码栏不能为空", "注意", JOptionPane.INFORMATION_MESSAGE);

            } else {
                list.add(people);
                BeginPanel frame = new BeginPanel(list, belongs);
                frame.setSize(900, 700);
                frame.setTitle("欢迎使用通讯录管理系统");
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                dispose();
            }

        }
    }

    class bgPanel extends JPanel {

        ImageIcon bg = new ImageIcon();

        bgPanel(String path) {
            bg = new ImageIcon(path);
        }

        bgPanel() {

        }

        @Override
        public void paint(Graphics g) {
            super.paintComponent(g);

            g.drawImage(bg.getImage(), 0, 0, null);
        }

        void setbg(String path) {
            bg = new ImageIcon(path);
        }
    }

    class bbgPanel extends JPanel {

        ImageIcon bg = new ImageIcon("img/bg_newPeople.png");

        @Override
        public void paint(Graphics g) {
            super.paintComponent(g);

            g.drawImage(bg.getImage(), 0, 0, null);
        }

    }


}
